package com.example.drug.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.example.common.cqsupervision.dto.req.CaVerifyReq;
import com.example.common.entity.EnumConfig;
import com.example.common.support.Result;
import com.example.common.utils.RequestHelp;
import com.example.drug.dto.req.RxListReq;
import com.example.drug.dto.req.RxMainReq;
import com.example.drug.dto.req.UnCheckRecipeReq;
import com.example.drug.dto.req.XRxListReq;
import com.example.drug.dto.resp.DiagnosisListRsp;
import com.example.drug.dto.resp.OrderPatients;
import com.example.drug.dto.resp.PharmacistDetail;
import com.example.drug.service.PatientService;
import com.example.drug.service.PrescriptionService;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "")
@RequestMapping("/api/Prescription")
public class PrescriptionController {

    @Resource
    private PrescriptionService prescriptionService;

    @Resource
    PatientService patientService;

    @ApiOperation(value = "待审核处方", notes = "待审核处方")
    @PostMapping(value = "/getPharmacistMsg")
    public Result<Map<String, Object>> getPharmacistMsg(@RequestHeader Long UID, @RequestBody UnCheckRecipeReq req) {
        return Result.success(prescriptionService.getPharmacistMsg(UID, req));
    }

    @ApiOperation(value = "处方详情", notes = "处方详情")
    @PostMapping(value = "/getPharmacistdetails")
    public Result<PharmacistDetail> getPharmacistdetails(@RequestHeader Long UID, @RequestBody RxMainReq re) {
        return Result.success(prescriptionService.getPharmacistdetails(UID, re.getRxOrderNO()));
    }

    @ApiOperation(value = "患者信息", notes = "患者信息")
    @PostMapping(value = "/getPatientsInfo")
    public Result<Map<String, Object>> getPatientsInfo(@RequestBody RxMainReq re) {
        return Result.success(prescriptionService.getPatientsInfo(re.getPID()));
    }

    @ApiOperation(value = "审核处方", notes = "审核处方")
    @PostMapping(value = "/UpdateRxMain")
    public Result<String> UpdateRxMain(@RequestHeader Long UID, @RequestBody RxMainReq re) {
        return Result.success(prescriptionService.UpdateRxMain(UID, re));
    }

    @ApiOperation(value = "就诊详情", notes = "就诊详情")
    @PostMapping(value = "/getOrderPatientsInfo")
    public Result<OrderPatients> getOrderPatientsInfo(@RequestBody RxMainReq re) {
        return Result.success(prescriptionService.getOrderPatientsInfo(re.getPID(), re.getOrderNO()));
    }

    @ApiOperation(value = "处方统计", notes = "处方统计")
    @PostMapping(value = "/getPrescription")
    public Result<HashMap<String, Object>> getPrescription(@RequestHeader Long UID, @RequestBody RxMainReq re) {
        return Result.success(prescriptionService.getPrescription(re.getPage(), re.getLimit(), re.getChkStatus(), UID));
    }

    @ApiOperation(value = "审方未通过原因配置", notes = "审方未通过原因配置")
    @PostMapping(value = "/getEnumConfig")
    public Result<List<EnumConfig>> getEnumConfig() {
        return Result.success(prescriptionService.getEnumConfig());
    }

    //#region Pc
    @ApiOperation(value = "Pc处方统计")
    @PostMapping(value = "/rxStatistics")
    public Result<Map<String, Object>> rxStatistics() {
        return Result.success(prescriptionService.rxStatistics());
    }

    @ApiOperation(value = "Pc处方列表")
    @PostMapping(value = "/rxList")
    public Result<Map<String, Object>> rxList(@RequestBody RxListReq req) {
        return Result.success(prescriptionService.rxList(req));
    }

    @ApiOperation(value = "病人就诊列表")
    @PostMapping("/DiagnosisList")
    public Result<PageInfo<DiagnosisListRsp>> DiagnosisList(@RequestBody XRxListReq xRxListReq) {
        PageInfo<DiagnosisListRsp> pageInfo = patientService.DiagnosisList(xRxListReq);
        return Result.success(pageInfo);
    }

    @ApiOperation(value = "PcCA信息")
    @PostMapping("/getCaOrder")
    public Result<Map<String,Object>> getCaOrder(HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String rxorderno = Jo.getString("rxOrderNo");
        return Result.success(prescriptionService.getCaOrder(rxorderno));
    }

    /**
     * 获得当前用户的激活码
     *
     * @param phone 用户的手机号
     * @return 返回用户激活信息
     */
    @GetMapping("/getCaMessage")
    @ApiOperation(value = "获得当前用户的激活码")
    public Result<Map<String, String>> getCaMessage(String phone) {
        return Result.success(prescriptionService.getCaMessage(phone));
    }

    /**
     * 提交签名任务
     *
     * @param orderId 订单id
     * @return 返回任务id
     */
    @GetMapping("/addSignJob")
    @ApiOperation("获得签名任务id")
    public Result<Map<String, String>> addSignJob(@ApiParam(required = true, name = "orderId", value = "订单id")
                                                          String orderId,
                                                  @ApiParam(required = true, name = "msspId", value = "用户编号")
                                                          String msspId) {
        Map<String, String> result = prescriptionService.addSignJob(orderId, msspId);

        return Result.success(result);
    }

    /**
     * 查询签名结果
     *
     * @param signJobId 签名任务id
     * @param msspId    用户编号
     * @return 返回任务结果
     */
    @GetMapping("/querySignJob")
    @ApiOperation("获得签名结果")
    public Result<Map<String, String>> querySignJob(@ApiParam(required = true, name = "signJobId", value = "任务id")
                                                            String signJobId,
                                                    @ApiParam(required = true, name = "msspId", value = "用户编号")
                                                            String msspId,
                                                    @ApiParam(required = true, name = "orderId", value = "订单id")
                                                            String orderId) {

        return Result.success(prescriptionService.querySignJob(signJobId, msspId, orderId));
    }

    /**
     * 获取是否签名成功
     *
     * @param caVerifyReq 签名结果封装类
     * @return 是否成功
     */
    @PostMapping("/verifySign")
    @ApiOperation("校验签名是否成功")
    public Result<Map<String, String>> verifySign(@ApiParam(required = true, value = "caVerifyReq", name = "签名结果请求类")
                                                  @RequestBody CaVerifyReq caVerifyReq) {

        return Result.success(prescriptionService.verifySign(caVerifyReq.getPlain(), caVerifyReq.getSignResult(), caVerifyReq.getCert(), caVerifyReq.getOrderId(), caVerifyReq.getTimeSignature()));
    }
    
    @ApiOperation(value = "是否已CA签名", notes = "")
    @PostMapping("/isCaSess")
    public Result<Boolean> isCaSess(HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String rxOrderNo = Jo.getString("rxOrderNo"); 

        return Result.success(prescriptionService.isCaSess(rxOrderNo));
    }  
}

